
public class MessageEngine {
		
	public MessageEngine(){
		
	}
	

	public MemberProfile InviteHandler(MemberProfile profile, String message){
		
		if (message.matches("^Call-ID.*")) {
			profile.setCall_ID(message);
		} else if (message.matches("^CSeq:.*")) {
			profile.setCSeq(message);
		} else if (message.matches("^From.*")) {
			profile.setFrom(message.substring(5).trim());
			profile.setName(message.replaceFirst("F.*:","").replaceFirst("@.*", ""));
			/*String temp2[] = splitMouse(From);
			 
			System.out.println(temp2[0]);
			String temp3[] = splitColumn(temp2[0]); 
			Name = temp3[2].trim();
			  
			String temp4[] = splitSpace(From); From = temp4[1].trim();*/
			 

		} else if (message.matches("^To.*")) {

			profile.setTo(message.substring(4).trim() + ";tag=" + (int) (Math.random() * 100000000 * 100));
			/*
			 * String temp2[] = splitSpace(message); To = temp2[2].trim() +
			 * ";tag=" + (int) (Math.random() * 100000000 * 100);
			 */
		} else if (message.matches("^Via.*")) {
			profile.setVia(message);
		} else if (message.matches("^Max.*")) {
			profile.setMaxForwards(message);
		} else if (message.matches("^Contact.*")) {
			profile.setContact(message);
		} else if (message.matches("^Content-Type.*")) {
			
		} else if (message.matches("^Content-Length.*")) {

		} else if (message.matches("^o=-.*")) {
			String[] temp = message.split(" ");
			profile.seto(Long.parseLong(temp[1]), Long.parseLong(temp[2]));
		} else if (message.matches("^m=.*")) {
			profile.setRTPPort(Integer.parseInt(message.split(" ")[1]));
		}
		
		return profile;
	}
	
	public MemberProfile SDPHandler(MemberProfile profile, String message){
		
		if(message.matches("^o=-.*")) {
			message.replace("192.168.1.*", "192.158.1.5");
		}
		profile.SDPaddString(message);
		return profile;
	}
	
	public String ResponseHandler(MemberProfile profile, ServerProfile serverprofile){
		String sip = null;
		String sdp;
		/*sdp = "\nv=0\no=- " + (profile.geto()[0]-1) + " " + (profile.geto()[1]+1) + " IN IP4 " + serverprofile.getIPAddress()
				+ "\ns=-\nc=IN IP4 " + serverprofile.getIPAddress() + "\nt=0 0\nm=audio "
				+ serverprofile.getRTPPort() + " RTP/AVP 96\na=rtpmap:96 GSM-EFR/8000\n";
		sip = "SIP/2.0 200 OK\n" + profile.getCall_ID() + "\n"+profile.getCSeq() + "\nFrom: " + profile.getFrom() + 
				"\nTo: " + profile.getTo() + "\n" + profile.getVia() + "\n" + profile.getMaxFowards()
				+ "\nContact: <sip:null@" + serverprofile.getIPAddress() + ":" + serverprofile.getPort()
				+ ";transport=udp>\nContent-type: application/sdp\nContent-Length: " + sdp.length() + "\n" + sdp;*/
		
		sip = "SIP/2.0 200 OK\r\n" + profile.getCall_ID() + "\r\n"+profile.getCSeq() + "\r\nFrom: " + profile.getFrom() + 
				"\r\nTo: " + profile.getTo() + "\r\n" + profile.getVia() + "\r\n" + profile.getMaxFowards()
				+ "\r\nContact: <sip:server@" + serverprofile.getIPAddress() + ":" + serverprofile.getPort()
				+ ";transport=udp>\r\nContent-Length: 0\r\n";
		
		
		return sip;
	}
	
	public String Trying(MemberProfile profile, ServerProfile serverprofile) {
		
		/*String sdp = "\r\nv=0\r\no=- " + (profile.geto()[0]) + " " + (profile.geto()[1]) + " IN IP4 " + serverprofile.getIPAddress()
				+ "\r\ns=-\r\nc=IN IP4 " + serverprofile.getIPAddress() + "\r\nt=0 0\r\nm=audio "
				+ serverprofile.getRTPPort() + " RTP/AVP 96\r\na=rtpmap:96 GSM-EFR/8000\r\n";*/
		
		
		return "SIP/2.0 100 trying\r\n" + profile.getCall_ID() + "\r\n"+profile.getCSeq() + "\r\nFrom: " + profile.getFrom() + 
				"\r\nTo: " + profile.getTo().split(";")[0] + "\r\n" + profile.getVia() + "\r\n" + profile.getMaxFowards()
				+ "\r\n" + profile.getContact() + "\r\n" + profile.getSDP();
				//+ "\r\nContent-type: application/sdp\r\nContent-Length: " + sdp.length() + "\r\n" + sdp; 
	}
	
	public String Ringing(MemberProfile profile, ServerProfile serverprofile) {
		
		return "SIP/2.0 180 ringing\r\n" + profile.getCall_ID() + "\r\n"+profile.getCSeq() + "\r\nFrom: " + profile.getFrom() + 
				"\r\nTo: " + profile.getTo().split(";")[0] + "\r\n" + profile.getVia() + "\r\n" + profile.getMaxFowards()
				+ "\r\n" + profile.getContact()
				+ "\r\nContent-Length: 0\r\n";
		
		
		
		
		/*return "SIP/2.0 180 ringing\r\n" + profile.getCall_ID() + "\n"+profile.getCSeq() + "\nFrom: " + profile.getFrom() + 
				"\nTo: " + profile.getTo().split(";")[0] + "\n" + profile.getVia() + "\n" + profile.getMaxFowards()
				+ "\n" + profile.getContact()
				+ "\nContent-Length: 0\n";*/ 
	}

	public String[] splitNextLine(String inputString){
		System.out.println(inputString);
		String outputString[] = inputString.split("/n");
		System.out.println(outputString[0]);
		return outputString;
	}
	
	public String[] splitSpace(String inputString){
		String outputString[] = inputString.split(" ");
		return outputString;
	}
	
	public String[] splitMouse(String inputString){
		String outputString[] = inputString.split("@");
		return outputString;
	}
	
	public String[] splitColumn(String inputString){
		String outputString[] = inputString.split(":");
		return outputString;
	}
}
